﻿using Common.Contract;
using Common.Contract.Authentication;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Service;
using SMECustomerWebsite.Core.Configuration;
using System.Web;
using System.Web.Mvc;

namespace SMECustomerWebsite.Filters
{
    public class CustomActionFilter : ActionFilterAttribute, IActionFilter
    {
        void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
        {
            // TODO: Add your acction filter's tasks here

            // Log Action Filter Call
            using (ServiceClient<IAuthentication> serviceClient = new ServiceClient<IAuthentication>())
            {
                ActionLogRecord log = new ActionLogRecord()
                {
                    Controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,
                    Action = filterContext.ActionDescriptor.ActionName,
                    Ip = filterContext.HttpContext.Request.UserHostAddress,
                    Datetime = filterContext.HttpContext.Timestamp,
                    Username = HttpContext.Current.User.Identity.Name
                };
                SaveActionLogRequest request = new SaveActionLogRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.SaveLog"
                    },
                    Record = log
                };
                var response = serviceClient.Proxy.SaveActionLog(request);
            }

            this.OnActionExecuted(filterContext);
        }
    }
}